﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class WBBSTController
    {
        [ChildActionOnly]
        public ActionResult SSTKRAB05(string returnUrl)
        {

            ModelState.Clear();
            MABF.CWBB.SLO = "01";
            MABF.CWBB.SLB = "PB";
            MABF.CWBB.LO = "01";
            MABF.CWBB.LB = "HZ";
            MABF.CWBB.LP = "01    ";
            MABF.CWBB.WHP = MABF.CWBB.LO + MABF.CWBB.LB + MABF.CWBB.LP;
            MABF.TERMNO = TERMNO;
            MABF.STAFF = CUSER;
            return PartialView(MABF.CWBB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SSTKRAB05(WBBSTModel model, string returnUrl)
        {
            try
            {
                SETTP(model);//指定布卷的托盘编号   
                ModelState.Clear();
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        public void SETTP(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;
            MABF.CWBB.WBBSTKNR = Helper.Left(model.WBBSTKNR.PadRight(12), 12);
            dwData = VHP.VWBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = MABF.CWBB.WBBSTKNR.PadRight(12); ;
            SResult = VHP.VWBB.QWB13(dwData);
            DataRow dwWB = null;
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                dwWB = row;
                break;
            }
            if (dwWB == null)
            {
                INFO = "布卷不存在或者品种信息不一致";
                return;
            } 
            PB2HZ(dwWB);   
        }
        public void PB2HZ(DataRow dwWB)
        {
             
          
            MWB.IsDataValid = true;
            DataRow _dwWB = SResult.Tables[0].Copy().Rows[0];
            double mtr = Convert.ToDouble(_dwWB["WBBISTKLG"].ToString()) * 0.001;
            Helper.GetSTime(ref date, ref time, ref stime);
            _dwWB["WBBAUFTYP"] = "V";
            _dwWB["WBBAUFKRS"] =dwWB["WBBAUFKRS"];
            _dwWB["WBBAUFLFD"] =dwWB["WBBAUFLFD"];
            _dwWB["WBBAUFPOS"] = dwWB["WBBAUFPOS"];
            _dwWB["WBBTP"] ="0001";
            _dwWB["WBBFOLGE"] = Helper.Right("01", 2);
            _dwWB["WBBGEBNR"] = "".PadRight(8);
            _dwWB["WBBAENDBEN"] = CUSER;
            _dwWB["WBBAENDDAT"] = date;
            PM["WBROW"] = _dwWB;
            PM["RUNID"] = RUNID;
            VHP.VWBB.WBB61(PM);//先留位，再转移  
            VHP.VWBB.MOVE2PB(_dwWB, MABF.CWBB.LO, MABF.CWBB.LB, MABF.CWBB.LP, this.RUNID);
        }
    }
}
